package org.suigara.d3.view;

import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ImageUI extends JPanel {

	private static final long serialVersionUID = 1L;
	private ImageIcon image;
	private int imgWidth;
	private int imgHeight;

	public ImageUI(int imgWidth, int imgHeight) {
		this(null, imgWidth, imgHeight);
	}

	public ImageUI(ImageIcon image) {
		this(image, 26, 56);
	}

	public ImageUI(ImageIcon image, int imgWidth, int imgHeight) {
		super();
		this.image = image;
		this.imgWidth = imgWidth;
		this.imgHeight = imgHeight;
	}

	public ImageIcon getImage() {
		return image;
	}

	public void setImage(ImageIcon image) {
		this.image = image;
	}

	public int getImgWidth() {
		return imgWidth;
	}

	public int getImgHeight() {
		return imgHeight;
	}

	@Override
	public void paintComponent(Graphics g1) {
		int x = (getWidth()-imgWidth)/2;
		int y = (getHeight()-imgHeight)/2;
		Graphics g = (Graphics) g1;
		g.drawRect(x, y, imgWidth, imgHeight);
		g.setColor(getBackground());
		g.fillRect(x, y, imgWidth, imgHeight);
		if (null == image) {
			return;
		}
		g.drawImage(image.getImage(), x, y, imgWidth, imgHeight, this);
		g = null;
	}
}
